package at.fhj.ase.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 
 * @author Zandmiralvand, Jaklitsch, Mansouri
 * 
 * This class stores information obtained from the PHPIDS rules
 *
 */

public final class Filter {
	
	private Integer id;
	private String description;
	private Integer impact;
	private List<String> tags;
	private Pattern filterRule;
	
	public Filter(Integer id, String description, Integer impact,
			List<String> tags, Pattern filterRule) {
		setId(id);
		setDescription(description);
		setImpact(impact);
		setTags(tags);
	}
	
	public Filter(){
		tags = new ArrayList<String>();
	}
	
	/**
	 * @param filterToCopy
	 * 
//	 * copy constructor, allows to create a deep copy of a <code>Filter</code> object
	 * to avoid, that the original object can be modified from outside
	 */
	public Filter(Filter filterToCopy) {
		this(filterToCopy.getId(), filterToCopy.getDescription(), filterToCopy.getImpact(),
				filterToCopy.getTags(), filterToCopy.getFilterRule());
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		if(id==null)
			throw new IllegalArgumentException("id mustn't be null");
		this.id = id;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		if(description==null)
			throw new IllegalArgumentException("description mustn't be null");
		this.description = description;
	}
	public Integer getImpact() {
		return impact;
	}
	public void setImpact(Integer impact) {
		if(impact==null)
			throw new IllegalArgumentException("impact mustn't be null");
		this.impact = impact;
	}
	
	public List<String> getTags(){
		return Collections.unmodifiableList(tags);
	}
	
	public void setTags(List<String> tags){
		if(tags==null || tags.size()==0)
			throw new IllegalArgumentException("tags mustn't be null or empty");
		this.tags=tags;
	}
	
	public void addTag(String tag){
		if(! tags.contains(tag)){
			tags.add(tag);
		}
	}

	//TODO: consider if filter rule should be unmodifiable from outside
	public Pattern getFilterRule() {
		
		return this.filterRule;
	}

	public void setFilterRule(String filterRule) {
		this.filterRule = Pattern.compile(filterRule);
//		Matcher matcher = this.filterRule.matcher("127.0.0.1");
//		if(matcher.matches())
//			{
//				//to something
//			this.detectedAttacks(new Attack(filterRule, logLine, "access.log")
//				
//			
//			};
//		matcher = this.filterRule.matcher("192.168.0.1");
//		"127.0.0.1".matches("^[0-9]*$")
			
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((impact == null) ? 0 : impact.hashCode());
		result = prime * result + ((tags == null) ? 0 : tags.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Filter other = (Filter) obj;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (impact == null) {
			if (other.impact != null)
				return false;
		} else if (!impact.equals(other.impact))
			return false;
		if (tags == null) {
			if (other.tags != null)
				return false;
		} else if (!tags.equals(other.tags))
			return false;
		return true;
	}

	//TODO: add the pattern (rule)
	@Override
	public String toString() {
		StringBuilder buffer = new StringBuilder();
		buffer.append("id: ").append(getId()).append(" description: ").append(getDescription())
			.append(" impact: ").append(getImpact()).append(" tags: ").append(getTags())
			.append(" regular expr:").append(getFilterRule().pattern());
		
		return buffer.toString();
	}	
}
